package com.mobilesalon.activity;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobilesalon.MobileSalonApplication;
import com.mobilesalon.R;

public class MainReport extends Activity {
	private static final String TAG = MainReport.class.getName();

	private List<Iconable> icons;

	public MainReport() {
		icons = Arrays.asList(new Iconable[] { new MonthReport(), new DetailedMonthReport() });
	}

	//
	// Lifecycle
	//
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_report);
		GridView grid = (GridView) findViewById(R.id.main_report_grid);
		grid.setAdapter(new ImageAdapter(MobileSalonApplication.getAppContext(), icons));

		grid.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				icons.get(position).getClass();
				startActivity(new Intent(getApplicationContext(), icons.get(position).getClass()));
			}

		});

	}

	class Image {

	}

	class ImageAdapter extends BaseAdapter {

		private Context context;
		private List<Iconable> icons;

		public ImageAdapter(Context myContext, List<Iconable> icons) {
			if (icons == null) {
				throw new IllegalStateException("icons canot be null!");
			}
			this.context = myContext;
			this.icons = icons;
		}

		public int getCount() {
			return icons.size();
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View viewItem = convertView;

			if (convertView == null) {
				/* we define the view that will display on the grid */
				Iconable iconable = icons.get(position);

				// Inflate the layout
				LayoutInflater li = getLayoutInflater();
				viewItem = li.inflate(R.layout.main_report_item, null);

				// Add The Text!!!
				TextView tv = (TextView) viewItem.findViewById(R.id.main_report_item_text);
				tv.setText(iconable.getIconName());

				// Add The Image!!!
				ImageView iv = (ImageView) viewItem.findViewById(R.id.main_report_item_image);
				iv.setImageResource(iconable.getIconResource());

			}

			return viewItem;
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
	}
}